package main.newViz;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Date: 19.05.2008
 * Time: 19:44:56
 *
 * @author Denis DIR Rozhnev
 */
class GamePanel extends JPanel {
    private LegendPanel legendPanel;
    private DrawPanel drawPanel;

    GamePanel(Config config, Runner runner) {
        super(new BorderLayout());
        legendPanel = new LegendPanel(runner);
        add(legendPanel, BorderLayout.WEST);
        drawPanel = new DrawPanel(config);
        add(drawPanel, BorderLayout.CENTER);

    }

    class LegendPanel extends JPanel {
        private JButton startStopButton;
        private JButton stepButton;
        private JTextArea legendArea;

        private final ActionProcessor actionProcessor;
        private final Icon ICON_START = new ImageIcon(this.getClass().getResource("start.PNG"));
        private final Icon ICON_STOP = new ImageIcon(this.getClass().getResource("stop.PNG"));
        private final Icon ICON_STEP = new ImageIcon(this.getClass().getResource("step.PNG"));

        LegendPanel(Runner runner) {
            super(new GridBagLayout());
            actionProcessor = new ActionProcessor(runner);
            GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1,
                    .0, .0,
                    GridBagConstraints.CENTER, GridBagConstraints.NONE,
                    new Insets(10, 10, 10, 10),
                    0, 0);
            startStopButton = new JButton("Start", ICON_START);
            startStopButton.addActionListener(actionProcessor);
            Dimension prefSize = startStopButton.getPreferredSize();
            startStopButton.setPreferredSize(prefSize);
            add(startStopButton, c);

            stepButton = new JButton("Step", ICON_STEP);
            stepButton.addActionListener(actionProcessor);
            stepButton.setPreferredSize(prefSize);
            c.gridx = 1;
            add(stepButton, c);

            legendArea = new JTextArea("Legend:\n" +
                    "Defect \t[*] - [*] Defect\n" +
                    "Defect \t[*] - [*] Cooperate\n" +
                    "Cooperate \t[*] - [*] Defect\n" +
                    "Cooperate \t[*] - [*] Cooperate");
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 2;
            add(legendArea, c);

            c.gridx = 0;
            c.gridy = 2;
            c.weighty = 1.;
            add(Box.createVerticalStrut(0), c);

            actionProcessor.cmdStop();
        }

        class ActionProcessor implements ActionListener {
            private static final String CMD_START = "Start";
            private static final String CMD_STOP = "Stop";
            private static final String CMD_STEP = "Step";

            final Runner runner;

            ActionProcessor(Runner runner) {
                this.runner = runner;
            }

            public void actionPerformed(ActionEvent e) {
                System.out.println("actionPerformed() ActionCommand = " + e.getActionCommand());
                if (CMD_START.equals(e.getActionCommand())) {
                    cmdStart();
                    runner.start();
                } else if (CMD_STOP.equals(e.getActionCommand())) {
                    cmdStop();
                    runner.stop();
                } else if (CMD_STEP.equals(e.getActionCommand())) {
                    runner.step(1);
                }
            }

            public void cmdStart() {
//                        startStopButton.setActionCommand(CMD_STOP);
                startStopButton.setText(CMD_STOP);
                startStopButton.setIcon(ICON_STOP);
                stepButton.setEnabled(false);
            }

            public void cmdStop() {
//                        startStopButton.setActionCommand(CMD_START);
                startStopButton.setText(CMD_START);
                startStopButton.setIcon(ICON_START);
                stepButton.setEnabled(true);
            }
        }
    }
}
